package com.easycarebn.main.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.easycarebn.main.utils.ItemRadioSpin;
import com.timbacsi.main.R;

public class SpinAdapter extends ArrayAdapter<ItemRadioSpin> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<ItemRadioSpin> listData;
	private int idCheked;

	public SpinAdapter(Context mContext, int resource, List<ItemRadioSpin> models, int idChecked) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		layoutInflater = LayoutInflater.from(mContext);
		this.idCheked = idChecked;
	}

	public class ViewHolder {
		public TextView lblSale;
		public TextView lblHisAmount;
		public CheckBox radio;
	}

	@Override
	public ItemRadioSpin getItem(int position) {
	    return listData.get(position);
	}
	
	@Override
	public long getItemId(int position) {
	    return listData.get(position).getId();
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.radio = (CheckBox) convertView
					.findViewById(R.id.btnCheck);
	
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ItemRadioSpin item = getItem(position);
	
		viewHolder.radio.setText(item.getLabel());
		if(idCheked == item.getId()) {
		    viewHolder.radio.setChecked(true);
		} else {
		    viewHolder.radio.setChecked(false);
		}

		return convertView;
	}
}
